<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item aria-current="true" :to="{path:'/roles'}">权限管理</el-breadcrumb-item>
      <el-breadcrumb-item aria-current="false">权限列表</el-breadcrumb-item>
      <!--    <el-breadcrumb-item>活动详情</el-breadcrumb-item>-->
    </el-breadcrumb>

    <br>

    <el-card>

      <add-role-btn v-on:update="getRoleList()"></add-role-btn>

      <!--      <h3>Hello</h3>-->
      <el-table
          border
          stripe
          :data="roleList">
        <el-table-column type="index"></el-table-column>

        <!--    展开列    -->
        <el-table-column type="expand">
          <template #default="scope">
            <!--  {{ scope.row }}-->
            <!--     el-row 分为24列       -->
            <el-row class="table_align"
                    :class="['bd-bottom',index===0?'bd-top':'']"
                    v-for="(item0,index) in scope.row.children"
                    :key="item0.id">
              <!--    一级权限    -->
              <el-col
                  :span="5">
                <el-tag closable
                        @close="removeRightById(scope.row,item0.id)"> {{ item0.authName }}
                </el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>

              <!--    二、三级权限    -->

              <el-col
                  :span="19">
                <!--       渲染二级权限         -->
                <el-row
                    class="table_align"
                    v-for="(item1,index1) in item0.children"
                    :class="[index1===0?'':'bd-top']"
                    :key="item1.id">
                  <el-col :span="6"
                  >
                    <el-tag type="success" closable
                            @close="removeRightById(scope.row,item1.id)">{{ item1.authName }}
                    </el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <el-col :span="18">
                    <el-tag
                        closable
                        @close="removeRightById(scope.row,item2.id)"
                        type="warning" v-for="(item2,index2) in item1.children"
                        :key="item2.id">{{ item2.authName }}
                    </el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>

          </template>


        </el-table-column>
        <el-table-column label="角色名称" prop="roleName"></el-table-column>
        <!--        <el-table-column label="路径" prop="path"></el-table-column>-->
        <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
        <el-table-column label="操作" width="300px">
          <template #default="scope">
            <el-button size="mini" type="primary" icon="el-icon-edit"
                       @click="
                       queryRoleById(scope.row.id)">编辑
            </el-button>
            <el-button size="mini"
                       type="danger"
                       @click="deleteRole(scope.row.id)"
                       icon="el-icon-delete">删除
            </el-button>
           <role-set-btn v-on:update="getRoleList" :id="scope.row.id" :row="scope.row"></role-set-btn>
          </template>
        </el-table-column>

      </el-table>
    </el-card>

    <edit-role-btn :edit-obj="editRole" v-on:showEditDialog="showEditDialog"
                   :dialog-visible="editDialogVisit"></edit-role-btn>


  </div>
</template>

<script>

import axios from "axios";
import AddRoleBtn from "@/components/power/roles/AddRoleBtn";
import EditRoleBtn from "@/components/power/roles/EditRoleBtn";
import RoleSetBtn from "@/components/power/roles/RoleSetBtn";

export default {
  name: "Roles",
  components: {RoleSetBtn, EditRoleBtn, AddRoleBtn},
  data() {
    return {
      roleList: [],
      editDialogVisit: false,
      addDialogVisit: false,
      editRole: {}
    }
  },
  methods: {
    async getRoleList() {
      const {data: res} = await axios.get('roles')

      if (res.meta.status !== 200) {
        return this.$message.error("权限列表获取失败")
      }

      console.log(res)
      this.roleList = res.data
    },
    showEditDialog(display) {
      // console.log(display)
      console.log("parent close edit role")
      this.editDialogVisit = display
    },
    showAddDialog(display) {
      // console.log(display)
      console.log("parent close edit role")
      //  this.editDialogVisit = display
    },
    async queryRoleById(id) {

      const {data: res} = await axios.get("roles/" + id);
      console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error("角色获取失败")
      }
      this.showEditDialog(true);


      this.editRole = res.data


    },
    async deleteRole(id) {
      console.log(id)


      const conres = await this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(async () => {


        const {data: res} = await axios.delete('roles/' + id)
        console.log(res)
        if (res.meta.status !== 200) {
          this.$message({
            type: 'info',
            message: '删除失败!'
          });
          return
        }

        this.$message.success(res.meta.msg)

        this.$message({
          type: 'success',
          message: '删除成功!'
        });
        this.getRoleList()

      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });

      console.log(conres)


    },
    async removeRightById(row, rightid) {
      const result = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)

      if (result !== 'confirm') {
        return

      }

      console.log('confirm');
      const {data: res} = await axios
          .delete(`roles/${row.id}/rights/${rightid}`);
      console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error('删除失败')
      }

      // this.getRoleList()
      row.children = res.data;
    }
  },
  created() {
    this.getRoleList()
  }
}
</script>

<style lang="less" scoped>
.el-tag {
  margin: 7px;
}

.bd-top {
  border-top: 1px solid #eeeeee;
}

.bd-bottom {
  border-top: 1px solid #eeeeee;
}

.table_align {
  display: flex;
  align-items: center;
}
</style>